package com.watch.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.watch.system.common.util.DateUtils;
import com.watch.system.converter.CarAdConverter;
import com.watch.system.mapper.CarAdMapper;
import com.watch.system.model.entity.CarAd;
import com.watch.system.model.form.AdForm;
import com.watch.system.model.query.CarAdPageQuery;
import com.watch.system.model.vo.CarAdExportVO;
import com.watch.system.model.vo.CarAdImportVO;
import com.watch.system.model.vo.CarAdPageVO;
import com.watch.system.model.vo.CarUserPageVO;
import com.watch.system.service.CarAdService;
import com.watch.system.service.CarUserService;
import com.watch.system.service.SysMenuService;
import com.watch.system.service.SysRoleService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户业务实现类
 *
 * @author haoxr
 * @since 2022/1/14
 */
@Service
@RequiredArgsConstructor
public class CarAdServiceImpl extends ServiceImpl<CarAdMapper, CarAd> implements CarAdService {


    private final CarAdConverter carAdConverter;


    @Autowired
    private CarUserService carUserService;

    /**
     * 获取用户分页列表
     *
     * @param queryParams
     * @return
     */
    @Override
    public IPage<CarAdPageVO> getAdPage(CarAdPageQuery queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CarAdPageVO> page = new Page<>(pageNum, pageSize);
        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        if(StringUtils.isNotEmpty(queryParams.getStartTime()) && StringUtils.isNotEmpty(queryParams.getEndTime())) {
            DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
        }
        // 查询数据
        Page<CarAdPageVO> carAdPage = this.baseMapper.selectAdPage(page, queryParams);
        carAdPage.getRecords().forEach(carAdPageVO -> {
            carAdPageVO.setCarUser(carUserService.getCarUser(carAdPageVO.getUserId()));
        });
        return carAdPage;
    }

    /**
     * 更新用户
     * @param adForm 用户表单对象
     * @return
     */
    @Override
    @Transactional
    public boolean updateAd(AdForm adForm) {
        // form -> entity
        CarAd entity = carAdConverter.form2Entity(adForm);
        // 修改用户
        int result=0;
        if(adForm.getAdId()!=null) {
            result = this.baseMapper.updateAd(adForm);
        }else{
            adForm.setCreateDate(new Date());
            adForm.setStatus("0");
            result = this.baseMapper.insertAd(adForm);
        }

        return result>0;
    }

    @Override
    public List<CarAdExportVO> listExportAds(CarAdPageQuery queryParams) {
        return this.baseMapper.listExportAds(queryParams);
    }

    @Override
    public boolean saveAdImport(CarAdImportVO carAdExportVO) {

        int result = this.baseMapper.insertAdImport(carAdExportVO);
        return result>0;
    }

    @Override
    public boolean deleteAd(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的广告数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(ids);
    }


}
